package hk.edu.cityu.is.riskmgmt.applets;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JApplet;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LCRCurrApplet extends JApplet {

	private static final long serialVersionUID = 2594874473564639267L;
	private double hkd;
	private double cny;
	private double usd;
	private double gbp;
	private double eur;
	private double jpy;
	private double cad;
	private double nzd;
	private double aud;
	private int tick;

	class LCDataGenerator extends Timer implements ActionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = -1103449238324643503L;

		public void actionPerformed(ActionEvent actionevent) {
			try {

				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				String connectionUrl = "jdbc:sqlserver://localhost:1438;database=HKBANK;user=sa;password=1111";
				Connection conn = DriverManager.getConnection(connectionUrl);
				Statement stmt = conn.createStatement(
						ResultSet.TYPE_SCROLL_SENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
				String sql1 = "select top 1 miles from dbo.last order by id desc";
				ResultSet rs = stmt.executeQuery(sql1);
				rs.next();
				long miles = rs.getLong("miles");
				String sql = "select * from dbo.last where miles=" + miles;
				// System.out.println(sql);
				rs = stmt.executeQuery(sql);
				// System.out.println(rs.getInt("miles"));
				while (rs.next()) {
					hkd = rs.getDouble("lcr_hkd");
					usd = rs.getDouble("lcr_usd");
					cny = rs.getDouble("lcr_cny");
					jpy = rs.getDouble("lcr_jpy");
					gbp = rs.getDouble("lcr_gbp");
					aud = rs.getDouble("lcr_aud");
					cad = rs.getDouble("lcr_cad");
					nzd = rs.getDouble("lcr_nzd");
					eur = rs.getDouble("lcr_eur");

					tick = rs.getInt("tick");
					System.out.println("hkd: " + hkd + ", usd:" + usd
							+ ", cny:" + cny + ", jpy" + jpy + ", gbp" + gbp
							+ ", aud" + aud + ", cad" + cad + ", nzd" + nzd
							+ ", eur" + eur);
					addUSDObservation(tick, usd);
					addCNYObservation(tick, cny);
					addGBPObservation(tick, gbp);
					addAUDObservation(tick, aud);
					addCADObservation(tick, cad);
					addAUDObservation(tick, aud);
					addNZDObservation(tick, nzd);
					addJPYObservation(tick, jpy);
					addEURObservation(tick, eur);

				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// System.out.println(lastValue1);

			// addFreeObservation(lastValue2);
		}

		LCDataGenerator() {
			super(100, null);
			addActionListener(this);
		}
	}

	public LCRCurrApplet() {
		// lastValue1 = 100D;

		usdxy = new XYSeries("usd");
		cnyxy = new XYSeries("cny");
		eurxy = new XYSeries("usd");
		gbpxy = new XYSeries("gbp");
		audxy = new XYSeries("aud");
		cadxy = new XYSeries("cad");
		nzdxy = new XYSeries("nzd");
		jpyxy = new XYSeries("jpy");
		hkdxy = new XYSeries("hkd");

		// total.setHistoryCount(30000);
		// free = new TimeSeries("Cpu01",
		// org.jfree.data.time.Millisecond.class);
		// free.setHistoryCount(30000);

		XYSeriesCollection xyseriecollection = new XYSeriesCollection();
		xyseriecollection.addSeries(usdxy);
		xyseriecollection.addSeries(eurxy);
		xyseriecollection.addSeries(gbpxy);
		xyseriecollection.addSeries(cnyxy);
		xyseriecollection.addSeries(hkdxy);
		xyseriecollection.addSeries(cadxy);
		xyseriecollection.addSeries(nzdxy);
		xyseriecollection.addSeries(jpyxy);
		xyseriecollection.addSeries(audxy);

		// timeseriescollection.addSeries(free);
		// BasicStroke basicstroke = new BasicStroke(2.0F, 0, 2);

		// DateAxis horizontaldateaxis = new DateAxis("Time");
		// NumberAxis verticalnumberaxis = new NumberAxis("Value");

		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(
				true, false);

		xylineandshaperenderer.setSeriesPaint(0, Color.red);
		xylineandshaperenderer.setSeriesPaint(1, Color.green);
		// xylineandshaperenderer.setStroke(new BasicStroke(3F, 0, 2));

		JFreeChart jfreechart = ChartFactory.createXYLineChart(
				"Concentration of Funding", "Tick", "Value", xyseriecollection,
				PlotOrientation.VERTICAL, true, true, false);

		XYPlot xyplot = jfreechart.getXYPlot();
		ValueAxis valueaxis = xyplot.getDomainAxis();
		valueaxis.setTickLabelsVisible(true);
		valueaxis.setAutoRange(true);
		// valueaxis.setFixedAutoRange(60000D);
		valueaxis = xyplot.getRangeAxis();
		// valueaxis.setTickLabelsVisible(false);
		valueaxis.setAutoRange(true);
		// valueaxis.setFixedAutoRange(20);
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		chartpanel.setPopupMenu(null);
		getContentPane().add(chartpanel);
		(new LCDataGenerator()).start();
	}

	private void addUSDObservation(int tick, double d) {

		usdxy.add(tick, d);
	}

	private void addCNYObservation(int tick, double d) {

		cnyxy.add(tick, d);
	}

	private void addEURObservation(int tick, double d) {

		eurxy.add(tick, d);
	}

	private void addAUDObservation(int tick, double d) {

		audxy.add(tick, d);
	}

	private void addCADObservation(int tick, double d) {

		cadxy.add(tick, d);
	}

	private void addNZDObservation(int tick, double d) {

		nzdxy.add(tick, d);
	}

	private void addGBPObservation(int tick, double d) {

		gbpxy.add(tick, d);
	}

	private void addJPYObservation(int tick, double d) {

		jpyxy.add(tick, d);
	}

	private void addHKDObservation(int tick, double d) {

		hkdxy.add(tick, d);
	}

	private XYSeries hkdxy;
	private XYSeries usdxy;
	private XYSeries cnyxy;
	private XYSeries eurxy;
	private XYSeries jpyxy;
	private XYSeries audxy;
	private XYSeries nzdxy;
	private XYSeries cadxy;
	private XYSeries gbpxy;

}